package com.zhuhjay.web.controller;

import com.zhuhjay.common.annotation.ApiLimit;
import com.zhuhjay.common.annotation.AuthToken;
import com.zhuhjay.common.annotation.OperationLog;
import com.zhuhjay.common.domain.ResponseJson;
import com.zhuhjay.common.domain.controller.BaseController;
import com.zhuhjay.common.enums.HttpCode;
import com.zhuhjay.system.domain.vo.PageInfo;
import com.zhuhjay.system.service.ILoginLogService;
import com.zhuhjay.system.service.IOperatorLogService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import java.util.List;

import static com.zhuhjay.common.domain.ResponseJson.error;
import static com.zhuhjay.common.domain.ResponseJson.success;

/**
 * @author ZhuHJay
 * @date 2022/3/27 21:43
 */
@Validated
@RestController
@RequestMapping("/system/log")
public class LogController extends BaseController {

    private final ILoginLogService loginLogService;
    private final IOperatorLogService operatorLogService;

    public LogController(ILoginLogService loginLogService,
                              IOperatorLogService operatorLogService){
        this.loginLogService = loginLogService;
        this.operatorLogService = operatorLogService;
    }

    @ApiLimit
    @AuthToken
    @GetMapping("/login")
    @OperationLog(operaDesc = "获取登录日志", hasResult = false)
    public ResponseJson getLoginLog(@RequestParam(name = "pageNum", defaultValue = "1")
                                    @Min(1) Integer pageNum,
                                    @RequestParam(name = "pageSize", defaultValue = "10")
                                    @Min(1) Integer pageSize,
                                    @RequestParam(name = "status", defaultValue = "-1")
                                    Integer status){
        sendBodyToSession(new PageInfo(pageNum, pageSize, status));
        if( status == 0 || status == 1 ){
            return success()
                    .appendData("loginLogPage", loginLogService.getLoginLogByStatus(pageNum, pageSize, status));
        }else{
            return success()
                    .appendData("loginLogPage", loginLogService.getLoginLog(pageNum, pageSize));
        }
    }

    @ApiLimit
    @AuthToken
    @DeleteMapping("/login")
    @OperationLog(operaDesc = "(可批量)删除登录日志")
    public ResponseJson deleteLoginLog(@RequestBody @NotEmpty(message = "错误请求") List<Integer> ids){
        sendBodyToSession(ids);
        loginLogService.deleteLoginLog(ids);
        return success(HttpCode.DELETE_SUCCESS);
    }

    @ApiLimit
    @AuthToken
    @DeleteMapping("/login/all")
    @OperationLog(operaDesc = "清空登录日志")
    public ResponseJson deleteLoginLogAll(){
        boolean remove = loginLogService.remove(null);
        if( !remove ){
            return error("清空失败, 请重试");
        }
        return success(HttpCode.DELETE_SUCCESS);
    }

    @ApiLimit
    @AuthToken
    @GetMapping("/operation")
    @OperationLog(operaDesc = "获取操作日志", hasResult = false)
    public ResponseJson getOperLog(@RequestParam(name = "pageNum", defaultValue = "1")
                                   @Min(1) Integer pageNum,
                                   @RequestParam(name = "pageSize", defaultValue = "10")
                                   @Min(1) Integer pageSize,
                                   @RequestParam(name = "status", defaultValue = "-1")
                                   Integer status){
        sendBodyToSession(new PageInfo(pageNum, pageSize, status));
        if( status == 0 || status == 1 ){
            return success()
                    .appendData("operationLogPage",
                            operatorLogService.getOperaLogByStatus(pageNum, pageSize, status));
        }else{
            return success()
                    .appendData("operationLogPage",
                            operatorLogService.getOperaLog(pageNum, pageSize));
        }

    }

    @ApiLimit
    @AuthToken
    @DeleteMapping("/operation")
    @OperationLog(operaDesc = "(可批量)删除操作日志")
    public ResponseJson deleteOperaLog(@RequestBody @NotEmpty(message = "错误请求") List<Integer> ids){
        sendBodyToSession(ids);
        operatorLogService.deleteOperaLog(ids);
        return success(HttpCode.DELETE_SUCCESS);
    }

    @ApiLimit
    @AuthToken
    @DeleteMapping("/operation/all")
    @OperationLog(operaDesc = "清空登录日志")
    public ResponseJson deleteOperaLogAll(){
        boolean remove = operatorLogService.remove(null);
        if( !remove ){
            return error("清空失败, 请重试");
        }
        return success(HttpCode.DELETE_SUCCESS);
    }
}
